package components.contorls.superClass
{
	import components.core.UIComponent;
	import components.managers.SystemManager;
	import components.utils.EventListenerUtil;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	import mx.events.FlexEvent;
	
	[Event(name="enter", type="mx.events.FlexEvent")]
	[Event(name="change", type="flash.events.Event")]
	[Event(name="textInput", type="flash.events.TextEvent")]
	public class BaseTextInput extends UIComponent
	{
		private var _text:String = "TextInput";
		
		private var _displayAsPassword:Boolean = false;
		
		private var _editable:Boolean = true;
		
		private var _maxChars:int = 0;
		
		private var _restrict:String = null;
		
		private var _textFormat:TextFormat;
		
		private var _color:Number = 0x000000;
		
		private var _showBorder:Boolean = false;
		
		private var _size:uint = 12;
		
		private var _bold:Boolean=false;
		
		public function BaseTextInput()
		{
			super();
			
			EventListenerUtil.addEventListener(textField,Event.CHANGE, changeHandler);
			EventListenerUtil.addEventListener(textField,KeyboardEvent.KEY_DOWN, keyDownHandler);
			EventListenerUtil.addEventListener(textField,TextEvent.TEXT_INPUT, textInputHandler);
			
		}
		
		/***************************************
		 *
		 * 显示对象声明
		 *
		 * **************************************/
		
		public function get textField():TextField
		{
			return getChildByName("tfSkin") as TextField;
		}
		
		/***************************************
		 *
		 * 验证
		 *
		 * **************************************/
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			
			if (!_initialized)
			{
				_textFormat = textField.defaultTextFormat;
				setTFFilter();
			}
			
			textField.text = text;
			textField.background=textField.border = showBorder;
			
			color=color;
		}
		
		protected override function measureSizes():Boolean
		{
			var change:Boolean=super.measureSizes();
			
			if (change)
			{
				textField.x = textField.y = 0;
				textField.width = width;
				textField.height = height;
			}
			
			return change;
		}
		
		
		protected override function updateDisplayList():void
		{
			super.updateDisplayList();
		}
		
		/***************************************
		 *
		 * 组件定义
		 *
		 * **************************************/
		
		[Inspectable(defaultValue="TextInput")]
		public function get text():String
		{
			return _text;
		}
		
		public function set text(value:String):void
		{
			if(value==null)
				_text="";
			else
				_text = value;
			invalidate();
		}
		
		[Inspectable(defaultValue="false")]
		public function get displayAsPassword():Boolean
		{
			return _displayAsPassword;
		}
		
		public function set displayAsPassword(value:Boolean):void
		{
			_displayAsPassword = value;
			textField.displayAsPassword = value;
		}
		
		[Inspectable(defaultValue="true")]
		public function get editable():Boolean
		{
			return _editable;
		}
		
		public function set editable(value:Boolean):void
		{
			_editable = value;
			
			if (value)
				textField.type = TextFieldType.INPUT;
			else
				textField.type = TextFieldType.DYNAMIC;
		}
		
		[Inspectable(defaultValue="0")]
		public function get maxChars():int
		{
			return _maxChars;
		}
		
		public function set maxChars(value:int):void
		{
			_maxChars = value;
			textField.maxChars = value;
		}
		
		[Inspectable(defaultValue="")]
		public function get restrict():String
		{
			return _restrict;
		}
		
		public function set restrict(value:String):void
		{
			if (value == null || value == "")
				_restrict = null;
			else
				_restrict = value;
			
			textField.restrict = _restrict;
		}
		
		
		[Inspectable(defaultValue="#000000", type="Color")]
		public function get color():Number
		{
			return _color;
		}
		
		public function set color(value:Number):void
		{
			_color = value;
			if (_textFormat)
				_textFormat.color = value;
			
			invalidateTextFormate();
		}
		
		[Inspectable(defaultValue="false")]
		public function get showBorder():Boolean
		{
			return _showBorder;
		}
		
		public function set showBorder(value:Boolean):void
		{
			_showBorder = value;
			invalidate();
		}
		
		[Inspectable(defaultValue="false")]
		public function get bold():Boolean
		{
			return 	_bold;
		}
		
		public function set bold(value:Boolean):void
		{
			_bold = value;
			
			if (_textFormat)
				_textFormat.bold = value;
			
			invalidateTextFormate();
		}
		
		[Inspectable(defaultValue="12")]
		public function get size():uint
		{
			return _size;
		}
		
		public function set size(value:uint):void
		{
			_size = value;
			if (_textFormat)
				_textFormat.size = value;
			invalidateTextFormate();
		}
		
		/***************************************
		 *
		 * 其他代码
		 *
		 * **************************************/
		
		protected function changeHandler(event:Event):void
		{
			event.stopImmediatePropagation();
			_text = textField.text;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		protected function keyDownHandler(event:KeyboardEvent):void
		{
			event.stopImmediatePropagation();
			if (event.keyCode == Keyboard.ENTER)
			{
				dispatchEvent(new FlexEvent(FlexEvent.ENTER));
			}
		}
		
		protected function textInputHandler(event:TextEvent):void
		{
			event.stopImmediatePropagation();
			
			var event:TextEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);
			dispatchEvent(event);
		}
		
		public function invalidateTextFormate():void
		{
			textField.defaultTextFormat = _textFormat;
			textField.setTextFormat(_textFormat);
		}
		
		public override function destroy():void
		{
			super.destroy();
			
			_text = null;
			_restrict = null;
			_textFormat = null;
		}
		
		public function replaceText(beginIndex:int, endIndex:int, newText:String):void
		{
			textField.replaceText(beginIndex, endIndex, newText);
			text = textField.text;
		}
		
		public function get selectionBeginIndex():int
		{
			return textField.selectionBeginIndex;
		}
		
		public function get selectionEndIndex():int
		{
			return textField.selectionEndIndex;
		}
		
		protected override function setTFFilter():void
		{
			if(tfFilterEnable)
				textField.filters = [ new GlowFilter(tfFilterColor, 1, 2, 2, 200)];
			else
				textField.filters=[];
		}
	}
}